#include "Mydefine.h"


void Camera2Init()
{
	uint16_t temp;
	uint8_t Flag = FAIL;
	// INIT START
ACKCAMERA2FAILED:
	// SEND ACK SIGNAL, CHECK THE CAMERA IS ALIVE 
	CmdCamera2(ACKCMD);
	while(0 != (Buffer_flg3 & 0x8000));
	for(temp = (Buffer_flg3 & 0x7FFF); temp > 4; --temp){
		if(('D' == Rcv_buffer3[temp]) && ('E' == Rcv_buffer3[temp - 2]) &&\
				('A' == Rcv_buffer3[temp - 4]) && ('A' == Rcv_buffer3[temp - 5])){
			Flag = SUCCESS;
			break;
		}
	}
	if(FAIL == Flag){
		Buffer_flg3 = 0x0000;
		goto ACKCAMERA2FAILED;
	}else{
		Flag = FAIL;
	}
INITCAMERA2FAILED:
	// SEND INIT COMMAND, SET THE JPEG PICTURE AS 640 X 480
	CmdCamera2(INITCMD);
	while(0 != (Buffer_flg3 & 0x8000));
	for(temp = (Buffer_flg3 & 0x7FFF); temp > 4; --temp){
		if(('1' == Rcv_buffer3[temp]) && ('E' == Rcv_buffer3[temp - 2]) &&\
				('A' == Rcv_buffer3[temp - 4]) && ('A' == Rcv_buffer3[temp - 5])){
			Flag = SUCCESS;
			break;
		}
	}
	if(FAIL == Flag){
		Buffer_flg3 = 0x0000;
		goto INITCAMERA2FAILED;
	}else{
		Flag = FAIL;
	}
INITDATASIZE2FAILED:
	// SEND DATASIZE INIT COMMAND, 0X0040 = 64B PER PACK
	CmdCamera2(DATASIZECMD);
	while(0 != (Buffer_flg3 & 0x8000));
	for(temp = (Buffer_flg3 & 0x7FFF); temp > 4; --temp){
		if(('6' == Rcv_buffer3[temp]) && ('E' == Rcv_buffer3[temp - 2]) &&\
				('A' == Rcv_buffer3[temp - 4]) && ('A' == Rcv_buffer3[temp - 5])){
			Flag = SUCCESS;
			break;
		}
	}
	if(FAIL == Flag){
		Buffer_flg3 = 0x0000;
		goto INITDATASIZE2FAILED;
	}else{
		Flag = FAIL;
	}
	// INIT COMPLETE, NOW CAMERA2 IS READY TO WORK
}

void Cmd_Send3(char num, ...)
{
  va_list Cmd_ptr;
  uint8_t i;
  va_start(Cmd_ptr, num);
	USART_ClearFlag(USART3, USART_FLAG_TC);
  while(0 < num--){
		i = va_arg(Cmd_ptr, int);
		USART_SendData(USART3, i);
		while(RESET == USART_GetFlagStatus(USART3, USART_FLAG_TC));
  }
  va_end(Cmd_ptr);
}

void Camera2Handler()
{
	
}
